<?php

namespace app\index\controller;

use app\common\model\Recharge as RechargeModel;
use app\common\model\Member as MemberModel;

/**
 * 财务明细控制器
 * Class Index
 * @package app\index\controller
 */
class Recharge extends Base
{
    /**
     * [getIndex  财务明细列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new RechargeModel;
        $mmodel = new MemberModel;
        try{
            if(!empty($param['type'])){
                if(!empty($param['con'])){
                    $map['m.tel|m.username'] = ['like', "%" . $param['con'] . "%"];
                }
                if(!empty($param['sex'])){
                    $map['m.sex'] = $param['sex'];
                }
                if(!empty($param['start']) && !empty($param['end'])){
                    $map['r.paytime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
                $map['r.comid'] = $user['comid'];
                $map['r.states'] = 1;
                $count = $model->getCounts($map);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $model->getLists($map, $this->from, $this->size);
                foreach($lists as &$v){
                    $v['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
                    if(empty($v['item_id'])){
                        $v['type'] = '结缘寺院';
                    }else{
                        $v['type'] = '乐善好施';
                    }
                    if($v['sex'] == 1){
                        $v['sex'] = '男';
                    }else{
                        $v['sex'] = '女';
                    }
                }
            }else{
                $map['comid'] = $user['comid'];
                $map['states'] = 1;
                $count = $model->getCount($map);//满足条件的总数
                $allpage = intval(ceil($count / $param['page_size']));//总页数
                $lists = $model->getList($map, $this->from, $param['page_size']);
                foreach($lists as &$v){
                    if(!empty($v['memid'])){
                        $member = $mmodel->findData($v['memid'],'id','tel,username,sex');
                        $v['tel'] = $member['tel'];
                        $v['username'] = $member['username'];
                        if($member['sex'] == 1){
                            $v['sex'] = '男';
                        }else{
                            $v['sex'] = '女';
                        }
                    }else{
                        $v['tel'] = '无';
                        $v['username'] = '匿名';
                        $v['sex'] = '无';
                    }
                    $v['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
                    if(empty($v['item_id'])){
                        $v['type'] = '结缘寺院';
                    }else{
                        $v['type'] = '乐善好施';
                    }
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  财务明细列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getUser()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new RechargeModel;
        try{
            if(!empty($param['con'])){
                $map['m.tel|m.username'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['sex'])){
                $map['m.sex'] = $param['sex'];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['r.paytime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['r.comid'] = $user['comid'];
            $map['r.states'] = 1;
            $map['r.memid'] = $param['memid'];
            $count = $model->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getLists($map, $this->from, $this->size);
            foreach($lists as &$v){
                $v['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
                if(empty($v['item_id'])){
                    $v['type'] = '结缘寺院';
                }else{
                    $v['type'] = '乐善好施';
                }
                if($v['sex'] == 1){
                    $v['sex'] = '男';
                }else{
                    $v['sex'] = '女';
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
}